//
//  FPSettleListController.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/2/19.
//

#import "FPSettleListController.h"
#import "FPSettleCell.h"
#import "FPSettleModel.h"
#import "FPSettleDetailController.h"

@interface FPSettleListController () <UITableViewDelegate , UITableViewDataSource , DZNEmptyDataSetSource , DZNEmptyDataSetDelegate>

@end

@implementation FPSettleListController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)initUI {
    
    self.view.backgroundColor = MAIN_VIEW_BACKGROUND_COLOR;
    
    [self setTitle:@"结清证明" style:(XYCustomNavigationStyleWhite) hiddenBackBtn:NO];
    
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.rowHeight = 82.f;
    self.tableView.frame = CGRectMake(0.f, UIDevice.navigationFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT - UIDevice.navigationFullHeight);
    self.tableView.backgroundColor = UIColor.clearColor;
    
    [self getData];
}

- (void)getData {
    
    [MBManager showLoading];
    [FPSettleModel getSettleListInfoSuccess:^(NSInteger code, NSString * _Nonnull message, NSArray * _Nonnull list) {
        [MBManager hideLoading];
        
        if (code == 200) {
            
            [self.dataSource removeAllObjects];
            
            for (FPSettleModel *model in list) {
                
                [self.dataSource addObject:model];
            }
            
            [self.tableView reloadData];
        }
        else {
            
            [self showText:message];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self showError:error];
    }];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    return IMG(@"bg_nodata_repayment");
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {

    return -80.f;
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    
    NSString *text = @"暂无数据";
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [NSString stringToAttribute:attStr font:FONT(12.f) range:NSMakeRange(0, text.length)];
    [NSString stringToAttribute:attStr color:[UIColor hexColor:@"#6A6A6A"] range:NSMakeRange(0, text.length)];

    return attStr;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPSettleCell *cell;
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(FPSettleCell.class) owner:nil options:nil].lastObject;
    }
    
    FPSettleModel *model = self.dataSource[indexPath.row];
    
    cell.titleLab.text = [NSString stringWithFormat:@"借款金额%@元", model.amount];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FPSettleModel *model = self.dataSource[indexPath.row];
    
    FPSettleDetailController *vc = [[FPSettleDetailController alloc] init];
    vc.loanID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
}



@end
